package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchant.service.acs.MerchantInfoService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchanth5.pojo.SignPo;
import cc.linker.steplink.crm.merchanth5.service.SignService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static cc.linker.steplink.crm.commons.utils.CharUtils.escapeQueryChars;

@RestController
@RequestMapping("/pc/sign")
public class SignController {

    private final UserInfoUtil userInfoUtil;
    private final MerchantInfoService merchantInfoService;
    private final SignService signService;

    public SignController(UserInfoUtil userInfoUtil, MerchantInfoService merchantInfoService, SignService signService) {
        this.userInfoUtil = userInfoUtil;
        this.merchantInfoService = merchantInfoService;
        this.signService = signService;
    }


    /**
     * 获取配置信息
     * @param request
     * @return
     */
    @GetMapping("/getinfo")
    public SignPo getInfo(HttpServletRequest request){
        PcUserInfo userInfo = userInfoUtil.getPcUserInfo(request);
        String orgId = userInfo.getOrgId();
        SignPo signpo = new SignPo();
        signpo.setOrgId(orgId);
        signpo.setCreateUser(orgId);
        if (userInfo.getSaler()) {
            String customerId = merchantInfoService.getCustomerIdByAdminId(userInfo.getAdminId());
            signpo.setCreateUser(customerId);
        }
        return signService.getInfo(signpo);
    }

    /**
     * 保存签到配置
     * @param signpo
     * @param request
     */
    @PostMapping("/save")
    public void save(@RequestBody SignPo signpo, HttpServletRequest request) {
        PcUserInfo userInfo = userInfoUtil.getPcUserInfo(request);
        String orgId = userInfo.getOrgId();
        signpo.setOrgId(orgId);
        signpo.setUpdateTime(new Date());
        signpo.setCreateUser(orgId);
        if(StringUtils.isEmpty(signpo.getCycle())){
            signpo.setCycle("0");
        }
        if(StringUtils.isEmpty(signpo.getScore())){
            signpo.setScore("0");
        }
        if(StringUtils.isEmpty(signpo.getGrowth())){
            signpo.setGrowth("0");
        }
        if (userInfo.getSaler()) {
            String customerId = merchantInfoService.getCustomerIdByAdminId(userInfo.getAdminId());
            signpo.setCreateUser(customerId);
        }
        signService.save(signpo);
    }

    /**
     * 签到统计
     * @param request
     * @return
     */
    @GetMapping("/getsigndata")
    public Map<String,Object> getSignData(HttpServletRequest request,
                                          @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                                          @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize){
        PcUserInfo userInfo = userInfoUtil.getPcUserInfo(request);
        String orgId = userInfo.getOrgId();
        SignPo signpo = new SignPo();
        signpo.setOrgId(orgId);
        return signService.getSignData(signpo,pageIndex,pageSize);
    }

    /**
     * 发放记录
     * @param request
     * @return
     */
    @GetMapping("/getsignsendlog")
    public PageInfo getSignSendLog(HttpServletRequest request,
                                   @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                                   @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                   @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
                                   @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
                                   @RequestParam(value = "nickName", defaultValue = "", required = false) String nickName,
                                   @RequestParam(value = "phone", defaultValue = "", required = false) String phone
                                             ){
        PcUserInfo userInfo = userInfoUtil.getPcUserInfo(request);
        String orgId = userInfo.getOrgId();
        Map<String,Object> map = new HashMap<>();
        map.put("orgId",orgId);
        if(!StringUtils.isEmpty(phone)){
            map.put("phone",phone);
        }
        if(!StringUtils.isEmpty(nickName)){
            map.put("nickName",escapeQueryChars(nickName));
        }

        if(!StringUtils.isEmpty(startTime)){
            map.put("startTime",startTime+" 00:00:00");
        }
        if(!StringUtils.isEmpty(endTime)){
            map.put("endTime",endTime+" 23:59:59");
        }
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(signService.getSignSendLog(map));
    }

}
